<template>
  <div class="content">
    <div>
      <MySearch :form-inline="formInline" @searchFn="searchFn">
        <template>
          <el-col :span="8">
            <el-form-item label="类型编号" prop="code">
              <el-input v-model="formInline.code" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类品名称" prop="name">
              <el-input v-model="formInline.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </template>
      </MySearch>
    </div>
    <div class="table">
      <div>
        <MyTable :table-data="locationList" :table-col="tableCol" :current="current" :size="size">
          <template #btn="props">
            <div style="display:flex;justify-content: space-between;">
              <el-button class="add" @click="addFn(props.status)">新增货品
              </el-button>
            </div>
          </template>
          <template #edit="props">
            <el-button
              type="text"
              size="small"
              @click="editFn(props.status)"
            >编辑</el-button>
            <el-button type="text" size="small" @click="delFn(props.status)">删除</el-button>
          </template>
        </MyTable>
      </div>
      <div>
        <MyPage :total-page="totalPage" @changeSize="changeSize" @changeCurrent="changeCurrent" />
      </div>
    </div>
    <!-- 新增修改弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" @closed="closeFn">
      <el-form ref="form" :model="form" :rules="formRoule">
        <el-form-item label="货品类型编码" prop="code">
          <el-input v-model="form.code" :disabled="true" clearable />
        </el-form-item>
        <el-form-item label="货品类型名称" prop="name">
          <el-input v-model="form.name" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="minorImportantButton unimportanceButton" @click="dialogFormVisible = false">取 消</el-button>
        <el-button class="minorImportantButton " type="primary" @click="yesAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import MySearch from '@/components/MySearch'
import MyTable from '@/components/MyTable'
import MyPage from '@/components/MyPage'
import { messageFn } from '@/utils/myutils'
import { goodsTypePage, delGoodsType, addGoodsType, nextFn, editGoodsType } from '@/api'
export default {
  components: { MySearch, MyTable, MyPage },
  data() {
    return {
      formInline: {},
      locationList: [],
      tableCol: [
        { prop: 'code', label: '货品类型编号', width: '334px' },
        { prop: 'name', label: '货品类型名称', width: '334px' },
        { prop: 'updateTime', label: '更新时间', width: '334px' }
      ],
      totalPage: null,
      size: 10,
      current: 1,
      dialogFormVisible: false,
      form: {},
      formRoule: {
        code: [{ required: true }],
        name: [{ required: true, message: '请输入货品类型名称', trigger: 'blur' }]
      }

    }
  },
  computed: {
    title() {
      return this.form.id ? '修改货品类型' : '添加货品类型'
    }
  },
  async created() {
    await this.warehouseFn(this.formInline, this.size, this.current)
  },
  methods: {
    // 请求
    async warehouseFn(obj, size, current) {
      const res = await goodsTypePage({
        ...obj,
        current,
        size,
        descs: 'createTime'
      })
      this.totalPage = Number(res.data.data.total)
      this.locationList = res.data.data.records
      return res.data
    },
    // 搜索
    async searchFn() {
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    // 改变页数
    async changeSize(val) {
      this.size = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    async changeCurrent(val) {
      this.current = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    async delFn(data) {
      this.$confirm(`确定要删除吗？`, `提示`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delGoodsType(data.id).then(() => {
          this.$message({
            type: 'success',
            message: `删除成功!`
          })
        }).catch(res => {
          console.log(res.response)
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
        await this.warehouseFn(this.formInline, this.size, this.current)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消删除`
        })
      })
    },
    async addFn() {
      this.form = {}
      const res = await nextFn('HPLX')
      this.form.code = res.data.data
      this.dialogFormVisible = true
    },
    editFn(val) {
      this.form = JSON.parse(JSON.stringify(val))
      this.dialogFormVisible = true
    },
    // 确定添加
    async yesAdd() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        let res
        if (this.form.id) {
          res = await editGoodsType(this.form)
        } else {
          res = await addGoodsType(this.form)
        }
        messageFn(res.data.isSuccess, this.title)
      })
      await this.warehouseFn(this.formInline, 10, 1)
      this.dialogFormVisible = false
    },
    closeFn() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';
@import '@/styles/common.scss';
.dialog-footer{
  display: flex !important;
  justify-content: end !important;
}
</style>
